package cn.xinfei.xdecision.engine.runner.executor;

import cn.xinfei.xdecision.common.model.enginex.po.EngineNode;
import cn.xinfei.xdecision.constant.EngineResultEnum;
import cn.xinfei.xdecision.engine.runner.api.DecisionDecisionApi;
import cn.xinfei.xdecision.engine.runner.context.PipelineContextHolder;
import cn.xinfei.xdecision.engine.runner.vo.EngineApiRequest;
import cn.xinfei.xdecision.model.DecisionApiResponse;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Slf4j
public class DecisionTreeNodeTest2 extends AbstractNode {


    @Autowired
    private DecisionDecisionApi decisionDecisionApi;

    @Test
    public void handler() {

        EngineApiRequest engineApiRequest = new EngineApiRequest();
        engineApiRequest.setEngineCode("bbb2d654-de75-4ad3-b364-f9e41a5327d7");

        Map<String, Object> input = new HashMap<>();
        input.put("idCard", "372925199010273333");
        input.put("mobile", "18710273386");
        input.put("aswer", 5);
        engineApiRequest.setIn(input);
        engineApiRequest.setRequestId(UUID.randomUUID().toString());

        PipelineContextHolder.setDecisionId(engineApiRequest.getRequestId());
        PipelineContextHolder.setRequestId(engineApiRequest.getRequestId());
        DecisionApiResponse response = null;
        try {
            response = decisionDecisionApi.execute(engineApiRequest);
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        String result = response.getCode();
        Assert.assertEquals(result, EngineResultEnum.SUCCESS_END.getCode());
        if (result.equals(EngineResultEnum.SUCCESS_END.getCode())) {
            log.info("引擎执行成功，result={}", result);
        } else {
            log.error("引擎执行失败，终止决策流,engineCode={},response={}", engineApiRequest.getEngineCode(), JSONObject.toJSONString(response));
//            engineNode.setNextNodeCodes(null);
        }
        log.info("---------------end----------------");
    }

    @Override
    public void build() {

    }


    protected EngineNode buildStartNode() {
        return null;
    }


    protected EngineNode buildEndNode() {
        return null;
    }
}